package top.hidoctor.auscultation.feature.fragment.device;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.tang.util.AppUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseFragment;
import top.hidoctor.auscultation.feature.activity.DeviceActivity;
import top.hidoctor.auscultation.feature.activity.MyXinyinActivity;

public class DeviceFragment extends BaseFragment implements View.OnClickListener {


    private LinearLayout llShebei, llXinyin;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initEvent();
    }

    private void initEvent() {

    }

    private void initView() {
        llShebei = getActivity().findViewById(R.id.ll_shebei);
        llXinyin = getActivity().findViewById(R.id.ll_xinyin);
        llShebei.setOnClickListener(this);
        llXinyin.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_shebei:
                if (!AppUtil.isFastClick()) {
                    new RxPermissions(getActivity()).request(new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION})
                            .subscribe(new Consumer<Boolean>() {
                                @Override
                                public void accept(Boolean aBoolean) throws Exception {
                                    if (aBoolean) {
                                        //申请的权限全部允许
                                        BluetoothAdapter blueadapter = BluetoothAdapter.getDefaultAdapter();
                                        if (!blueadapter.isEnabled()) {
//                                            showToast("蓝牙功能尚未打开，请打开蓝牙");
                                            blueadapter.enable();
                                        } else {
                                            startActivity(new Intent(getContext(), DeviceActivity.class));
                                        }
                                    } else {
                                        //只要有一个权限被拒绝，就会执行
                                        showToast("权限被禁止，无法使用蓝牙功能");
                                    }

                                }
                            });
                }

                break;
            case R.id.ll_xinyin:
                startActivity(new Intent(getContext(), MyXinyinActivity.class));
                break;

        }
    }

}
